#!/bin/bash
# autobackux - Backup your files
# Jon Peeters - June 2011

# autobackux is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

labels="Backup"

start_backup() {
	rm -f $rsync_log $dirs_file_tmp
	load_backups
	results="$_backup_title_results_"
	size=${#backups[@]}		
	for ((i=0; i<size; i++)); do
		parseRecord "${backups[$i]}"
		backup_dir
		record="${values[$DTE]}|${values[$NME]}|${values[$SRC]}|${values[$DST]}"
		backups[$i]="$record"
		echo "$record" >> $dirs_file_tmp
		results="$results\n$result_backup"
	done
	add_legend
	mv $dirs_file_tmp $dirs_file
	zenity --info --no-wrap --text="$font_zenity$results</span>"
}
 
backup_dir() {
	if [[ ! -d "${values[$SRC]}" ]]; then
		result_backup="$red_color$_backup_SNA_</span> ${values[$NME]}"
		rc_backup=1
		return
	fi
	test_source_uptodate
	if [[ $? -eq 0 ]]; then
		result_backup="$green_color$_backup_UPD_</span> ${values[$NME]}"
		rc_backup=0
		return
	fi
	if [[ ! -d "${values[$DST]}" ]]; then
		result_backup="$red_color$_backup_DNA_</span> ${values[$NME]}"
		rc_backup=1
		return
	fi
	#echo "rsync -au --progress --delete-after --log-file=rsync.log ${values[$SRC]}/ ${values[$DST]}"
	rsync -au --progress --delete-after --log-file="$rsync_log" "${values[$SRC]}/" "${values[$DST]}"
	if [[ $? -ne 0 ]]; then
		result_backup="$red_color$_backup_ERR_</span> ${values[$NME]}"
		rc_backup=1
		return
	fi
	result_backup="$green_color[ OK  ]</span> ${values[$NME]}"
	values[$DTE]=$(date +%s)
	chmod -R 755 "${values[$DST]}"
	rc_backup=0
}

test_source_uptodate() {
	if [[ "${values[$DTE]}" != "" ]]; then
		newer=$(find "${values[$SRC]}" -printf '%T@\n' | sort -rn |head -1)
		newer=${newer%.*}
		if [[ $newer -le ${values[$DTE]} ]]; then
			return 0
		fi
	fi
	return 1
}
 
backup_cdrom() {
	wait_for_cdrom
	if [[ "$cdrom" == "" ]]; then
		echo -e "\t* No cd/dvd => cancel"
		return
	fi
	name_cdrom=$(zenity --entry --text="$_backup_cd_text_" --title="")
	if [[ "$name_cdrom" == "" ]]; then
		echo -e "\t* No name for cd/dvd => cancel"
		return
	fi
	hget "$key_default_dest"
	if [[ "$values" == "" ]]; then
		fatal_error "$_no_default_dir_defined_"
	fi
	cdrom_destination="$values/$name_cdrom"
	if [[ ! -d "$cdrom_destination" ]]; then
		mkdir "$cdrom_destination"
		if [[ $? -ne 0 ]]; then
			display_error $(printf "$_backup_error_creating_dir_cd_" "$cdrom_destination")
			return
		fi
	fi
	values=( "$cdrom" "$cdrom_destination" "$name_cdrom" )
	backup_dir
	if [[ $rc_backup -ne 0 ]]; then
		display_error "$_backup_error_copy_cd_"
		return
	else
		zenity --info --text="$_backup_successfully_copy_cd_"
	fi
}

wait_for_cdrom() {
	dev=$(ls -l /dev/cdrom |awk '{ print $(NF) }')
	cdrom=$(mount |grep $dev |sed 's|.* on \(/media/.*\( ?type\)*\) type .*|\1|')
	while [[ "$cdrom" == "" ]]; do
		zenity --question --title="" --text="$_backup_waiting_for_cd_"
		if [[ $? -eq 0 ]]; then
			cdrom=$(mount |grep $dev |sed 's|.* on \(/media/.*\( ?type\)*\) type .*|\1|')
		else
			echo -e "\t* No cd/dvd => cancel"
			return
		fi
	done
}

add_legend() {
	results="$results\n_____________"
	results="$results\n$green_color$_backup_UPD_</span> $_backup_UPD_label_"	
	results="$results\n$red_color$_backup_SNA_</span> $_backup_SNA_label_"
	results="$results\n$red_color$_backup_DNA_</span> $_backup_DNA_label_"
	results="$results\n$red_color$_backup_ERR_</span> $_backup_ERR_label_"
}
